# from https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs

grid.arrange_shared_legend <- function(plots)
{
	g <- ggplotGrob(plots[[1]] + theme(legend.position="right"))$grobs
	legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
	lwidth <- sum(legend$width)
	grid.arrange(
		do.call(arrangeGrob, c(lapply(plots, function(x)
			x + theme(legend.position="none")), ncol=length(plots))),
		legend, ncol=2,
		widths=unit.c(unit(1,"npc") - lwidth, lwidth))
}
